<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="岗位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postName">
              <a-input disabled v-model="model.postName" placeholder="请输入岗位名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="员工" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
              <select-user-by-dep
                placeholder="请选择员工"
                v-model="model.employeeId"
                @back="handleChangeEmployee"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyId">
              <select-Company-input v-model="model.companyId" placeholder="请选择公司" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
              <select-team-input :query-type="'parent'" v-model="model.teamId" :tdlx="3" :teamType="0" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="任职时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workingTime">
              <j-date
                :disabled="formDisabled"
                placeholder="请选择任职时间"
                v-model="model.workingTime"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea :disabled="formDisabled" v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput';
import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
export default {
  name: 'ErpEmployeePostDetailListForm',
  components: {
    SelectUserByDep,
    SelectCompanyInput,
    SelectTeamInput
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
    },
    data () {
      return {
        model: {
          positionStatus: 'enabled',
          repeatSortHandleWay: 'insert'
        },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
          postCode: [
            { required: true, message: '请输入岗位名称!' }
          ],
          employeeId: [
            { required: true, message: '请选择员工!' }
          ],
          // companyId: [
          //   { required: true, message: '请选择公司!' }
          // ],
          // teamId: [
          //   { required: true, message: '请选择团队!' }
          // ],
          workingTime: [
            { required: true, message: '请选择任职时间!' }
          ]
        },
        url: {
          add: '/api/erp/files/employee/erpEmployeePostChangeRecord/add',
          edit: '/api/erp/files/employee/erpEmployeePostChangeRecord/edit',
          queryById: '/api/erp/files/employee/erpEmployeePostChangeRecord/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (detail) {
        this.edit(this.modelDefault, detail);
      },
      edit (record, detail) {
        if (record && !record.repeatSortHandleWay) {
          record.repeatSortHandleWay = 'insert'
        }
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.$set(this.model, 'isVirtual', detail.isVirtual) // 是否虚拟岗位
        this.$set(this.model, 'postCode', detail.code) // 岗位Code
        this.$set(this.model, 'postName', detail.name) // 岗位名称
        this.$set(this.model, 'positionCategory', detail.positionCategory) // 岗位序列
        this.$set(this.model, 'isDuty', '1') // 是否在岗
        this.$set(this.model, 'postDataSource', 'post') // 岗位的数据来源
        this.visible = true;
      },
      handleChangeEmployee (val) {
        if (val && val.length > 0) {
          this.$set(this.model, 'employeeId_dictText', val[0].realname)
        } else {
          this.$set(this.model, 'employeeId_dictText', '')
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      }
    }
  }
</script>
